package com.i2works.smartluolong.web.controller.common;

import com.google.common.collect.Lists;
import com.i2works.smartluolong.utils.base.CompanyConstant;
import com.i2works.smartluolong.utils.base.FlowItemTypeEnum;
import com.i2works.smartluolong.utils.base.GlobalConstant;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.flow.*;
import com.i2works.smartluolong.web.service.flow.IFlowProcessService;
import com.i2works.smartluolong.web.service.flow.IFlowService;
import com.i2works.smartluolong.web.service.flow.IFlowTemplateService;
import com.i2works.smartluolong.web.utils.BaseModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 工商公共控制层
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/5/4 11:11
 */

@Component
public class CommonItemController extends BaseModule {

    private final IFlowTemplateService flowTemplateService;

    private final IFlowService flowService;

    private final IFlowProcessService flowProcessService;

    @Autowired
    public CommonItemController(IFlowTemplateService flowTemplateService, IFlowService flowService, IFlowProcessService flowProcessService) {
        this.flowTemplateService = flowTemplateService;
        this.flowService = flowService;
        this.flowProcessService = flowProcessService;
    }

    /**
     * 查询流程进度
     *
     * @param itemId   事项编号
     * @param itemType 事项类型
     */
    public List<FlowProcessDO> queryFlowProcess(Integer itemId, String itemType) throws ServiceException {
        //查询流程
        FlowProcessQuery flowProcessQuery = new FlowProcessQuery();
        flowProcessQuery.setItemId(itemId);
        flowProcessQuery.setItemType(itemType);
        Integer queryCount = flowProcessService.queryCount(flowProcessQuery);
        if (queryCount > 0) {
            return flowProcessService.queryList(flowProcessQuery);
        }
        return null;
    }


    /**
     * 获取工作流模板
     */
    public FlowTemplateDO getFlowTemplate(Integer adminRoleId) throws ServiceException {
        //获取工作流
        FlowQuery flowQuery = new FlowQuery();
        flowQuery.setItemCode(FlowItemTypeEnum.COMPANY_APPLY.getKey());
        flowQuery.setStatus(GlobalConstant.status.TRUE);
        FlowDO flowDO = flowService.query(flowQuery);
        Integer flowId = flowDO.getId();
        //获取工作流模板
        FlowTemplateQuery templateQuery = new FlowTemplateQuery();
        templateQuery.setFlowId(flowDO.getId());
        templateQuery.setRoleId(adminRoleId);
        return flowTemplateService.query(templateQuery);
    }

    /**
     * 标示是否存在
     */
    public static boolean actionContains(String action) {
        return Lists.newArrayList(CompanyConstant.action.HANDLE, CompanyConstant.action.INDEX, CompanyConstant.action.INVALID, CompanyConstant.action.PASS, CompanyConstant.action.REFUSE).contains(action);
    }


}
